# coding:utf-8
"""
__author__="yanwei"
__describe__="新建营销计划（第二步）"
__API__="/plan/step/:id"
"""

import json
from lib.operate_excel import OperateExcel
from lib.run_main import RunMain
from conf.readConfig import ReadConfig
import os
import xlrd
from xlutils.copy import copy
from lib.font_colour import red_colour, green_colour
from tool_class.marketing_plan_stepone import MarketingPlanStepone
import requests
import random
from tool_class.marketing_plan_stepone import MarketingPlanStepone
import time
from tool_class.get_token import GetToken


class MarketingPlanSteptwo():
    def setUp(self):
        print("*************开始执行*************")

    def tearDown(self):
        print("-------------执行结束------------")

    def marketing_plan_step_two(self):
        # 获取计划创建第一步的计划名称、计划id、计划开始时间
        stepone = MarketingPlanStepone().marketing_plan_step_one()
        plan_id = stepone["plan_id"]
        plan_title = stepone["plan_title"]
        begin_time = stepone["begin_time"]

        # 获取token
        token = GetToken().mobile()


        # 获取当天结束时间
        end_time = time.strftime('%Y-%m-%d 23:59:59', time.localtime(time.time()))

        host = ReadConfig().get_url("test_http_url")
        # 请求参数存放地址
        case_path = os.path.join(os.path.join(os.path.dirname(os.path.split(os.path.realpath(__file__))[0]), "data"),
                                 "接口用例.xls")
        # 请求数据存放标签页
        sheet_name = "新建计划（第二步）添加计划内容"
        operateexcle = OperateExcel()
        data = json.loads(operateexcle.excel_all_param(case_path, sheet_name)[0][6])
        url = operateexcle.excel_all_param(case_path, sheet_name)[0][2]
        headers = {
            "Content-Type": json.loads(operateexcle.excel_all_param(case_path, sheet_name)[0][5])["Content-Type"],
            "authorization": token
        }
        request_param = {
            "info": {
                "title": plan_title,
                "type": data["info"]["type"],
                "return_type": data["info"]["return_type"],
                "reseller_id": data["info"]["reseller_id"],
                "reseller_name": data["info"]["reseller_name"],
                "begin_time": str(begin_time),
                "end_time": str(end_time)
            },
            "key": [{
                "batch_name": data["key"][0]["batch_name"],
                "style": data["key"][0]["style"],
                "quantity": data["key"][0]["quantity"],
                "bind_object": data["key"][0]["bind_object"],
                "allow_repetition": data["key"][0]["allow_repetition"],
                "allow_loss": data["key"][0]["allow_loss"],
                "merge_stock": data["key"][0]["merge_stock"],
                "code_batch": [{
                    "title": data["key"][0]["code_batch"][0]["title"],
                    "describe": "",
                    "begin_time": str(begin_time),
                    "end_time": str(end_time),
                    "quantity": data["key"][0]["code_batch"][0]["quantity"],
                    "restrict": data["key"][0]["code_batch"][0]["restrict"],
                    "range": data["key"][0]["code_batch"][0]["range"],
                    "product": {
                        "legal": [{
                            "product_id": data["key"][0]["code_batch"][0]["product"]["legal"][0]["product_id"],
                            "product_type": data["key"][0]["code_batch"][0]["product"]["legal"][0]["product_type"],
                            "product_name": data["key"][0]["code_batch"][0]["product"]["legal"][0]["product_name"],
                            "contract_price": data["key"][0]["code_batch"][0]["product"]["legal"][0]["contract_price"],
                            "cost_price": data["key"][0]["code_batch"][0]["product"]["legal"][0]["cost_price"],
                            "official_price": data["key"][0]["code_batch"][0]["product"]["legal"][0]["official_price"],
                            "quantity": data["key"][0]["code_batch"][0]["product"]["legal"][0]["quantity"],
                            "show_url": data["key"][0]["code_batch"][0]["product"]["legal"][0]["show_url"],
                            "describe_url": data["key"][0]["code_batch"][0]["product"]["legal"][0]["describe_url"],
                            "detail_url": "",
                            "account_type": data["key"][0]["code_batch"][0]["product"]["legal"][0]["account_type"],
                            "map_product_name": data["key"][0]["code_batch"][0]["product"]["legal"][0][
                                "map_product_name"],
                            "only": data["key"][0]["code_batch"][0]["product"]["legal"][0]["only"],
                            "weight": data["key"][0]["code_batch"][0]["product"]["legal"][0]["weight"],
                            "type": data["key"][0]["code_batch"][0]["product"]["legal"][0]["type"],
                            "upstream": data["key"][0]["code_batch"][0]["product"]["legal"][0]["upstream"]
                        }],
                        "reduce": []
                    }
                }, {
                    "title": data["key"][0]["code_batch"][1]["title"],
                    "describe": "",
                    "begin_time": str(begin_time),
                    "end_time": str(end_time),
                    "quantity": data["key"][0]["code_batch"][1]["quantity"],
                    "restrict": data["key"][0]["code_batch"][1]["restrict"],
                    "range": data["key"][0]["code_batch"][1]["range"],
                    "product": {
                        "legal": [{
                            "product_id": data["key"][0]["code_batch"][1]["product"]["legal"][0]["product_id"],
                            "product_type": data["key"][0]["code_batch"][1]["product"]["legal"][0]["product_type"],
                            "product_name": data["key"][0]["code_batch"][1]["product"]["legal"][0]["product_name"],
                            "contract_price": data["key"][0]["code_batch"][1]["product"]["legal"][0]["contract_price"],
                            "cost_price": data["key"][0]["code_batch"][1]["product"]["legal"][0]["cost_price"],
                            "official_price": data["key"][0]["code_batch"][1]["product"]["legal"][0]["official_price"],
                            "quantity": data["key"][0]["code_batch"][1]["product"]["legal"][0]["quantity"],
                            "show_url": data["key"][0]["code_batch"][1]["product"]["legal"][0]["show_url"],
                            "describe_url": data["key"][0]["code_batch"][1]["product"]["legal"][0]["describe_url"],
                            "detail_url": "",
                            "account_type": data["key"][0]["code_batch"][1]["product"]["legal"][0]["account_type"],
                            "map_product_name": data["key"][0]["code_batch"][1]["product"]["legal"][0][
                                "map_product_name"],
                            "only": data["key"][0]["code_batch"][1]["product"]["legal"][0]["only"],
                            "weight": data["key"][0]["code_batch"][1]["product"]["legal"][0]["weight"],
                            "type": data["key"][0]["code_batch"][1]["product"]["legal"][0]["type"],
                            "upstream": data["key"][0]["code_batch"][1]["product"]["legal"][0]["upstream"]
                        }],
                        "reduce": []
                    }
                }],
                "mobile_excel": "",
                "mobile_repeat": []
            }, {
                "batch_name": data["key"][1]["batch_name"],
                "style": data["key"][1]["style"],
                "quantity": data["key"][1]["quantity"],
                "bind_object": data["key"][1]["bind_object"],
                "allow_repetition": data["key"][1]["allow_repetition"],
                "allow_loss": data["key"][1]["allow_loss"],
                "merge_stock": data["key"][1]["merge_stock"],
                "code_batch": [{
                    "title": data["key"][1]["code_batch"][0]["title"],
                    "describe": "",
                    "begin_time": str(begin_time),
                    "end_time": str(end_time),
                    "quantity": data["key"][1]["code_batch"][0]["quantity"],
                    "restrict": data["key"][1]["code_batch"][0]["restrict"],
                    "range": data["key"][1]["code_batch"][0]["range"],
                    "product": {
                        "legal": [],
                        "reduce": [{
                            "channel": data["key"][1]["code_batch"][0]["product"]["reduce"][0]["channel"],
                            "temp_no": data["key"][1]["code_batch"][0]["product"]["reduce"][0]["temp_no"],
                            "batch_goods_name": data["key"][1]["code_batch"][0]["product"]["reduce"][0][
                                "batch_goods_name"],
                            "channel_activity_id": random.randint(11111111111, 99999999999999),
                            "price": data["key"][1]["code_batch"][0]["product"]["reduce"][0]["price"],
                            "recharge_amount": data["key"][1]["code_batch"][0]["product"]["reduce"][0][
                                "recharge_amount"],
                            "denomination": data["key"][1]["code_batch"][0]["product"]["reduce"][0]["denomination"],
                            "reduce_amount": data["key"][1]["code_batch"][0]["product"]["reduce"][0]["reduce_amount"],
                            "all_budget": data["key"][1]["code_batch"][0]["product"]["reduce"][0]["all_budget"],
                            "day_budget": data["key"][1]["code_batch"][0]["product"]["reduce"][0]["day_budget"],
                            "card_type": data["key"][1]["code_batch"][0]["product"]["reduce"][0]["card_type"],
                            "natural_limit": data["key"][1]["code_batch"][0]["product"]["reduce"][0]["natural_limit"],
                            "brush_limit": data["key"][1]["code_batch"][0]["product"]["reduce"][0]["brush_limit"],
                            "instruction": data["key"][1]["code_batch"][0]["product"]["reduce"][0]["instruction"],
                            "early_per": data["key"][1]["code_batch"][0]["product"]["reduce"][0]["early_per"],
                            "early_notifier": [{
                                "real_name":
                                    data["key"][1]["code_batch"][0]["product"]["reduce"][0]["early_notifier"][0][
                                        "real_name"],
                                "mobile": data["key"][1]["code_batch"][0]["product"]["reduce"][0]["early_notifier"][0][
                                    "mobile"]
                            }],
                            "weight": data["key"][1]["code_batch"][0]["product"]["reduce"][0]["weight"],
                            "receive_conf": {
                                "type": data["key"][1]["code_batch"][0]["product"]["reduce"][0]["receive_conf"]["type"],
                                "num": data["key"][1]["code_batch"][0]["product"]["reduce"][0]["receive_conf"]["num"]
                            },
                            "time_limit": {
                                "effect_time": {
                                    "start_time": str(begin_time),
                                    "end_time": str(end_time)
                                },
                                "receive_time": {},
                                "use_time": {
                                    "type": data["key"][1]["code_batch"][0]["product"]["reduce"][0]["time_limit"][
                                        "use_time"]["type"],
                                    "fiexd_time": {},
                                    "effect_day": data["key"][1]["code_batch"][0]["product"]["reduce"][0]["time_limit"][
                                        "use_time"]["effect_day"],
                                    "effect_date_type":
                                        data["key"][1]["code_batch"][0]["product"]["reduce"][0]["time_limit"][
                                            "use_time"]["effect_date_type"]
                                }
                            }
                        }]
                    }
                }],
                "mobile_excel": "",
                "mobile_repeat": []
            }, {
                "batch_name": data["key"][2]["batch_name"],
                "style": data["key"][2]["style"],
                "quantity": data["key"][2]["quantity"],
                "bind_object": data["key"][2]["bind_object"],
                "allow_repetition": data["key"][2]["allow_repetition"],
                "allow_loss": data["key"][2]["allow_loss"],
                "merge_stock": data["key"][2]["merge_stock"],
                "code_batch": [{
                    "title": data["key"][2]["code_batch"][0]["title"],
                    "describe": "",
                    "begin_time": str(begin_time),
                    "end_time": str(end_time),
                    "quantity": data["key"][2]["code_batch"][0]["quantity"],
                    "restrict": data["key"][2]["code_batch"][0]["restrict"],
                    "range": data["key"][2]["code_batch"][0]["range"],
                    "product": {
                        "legal": [{
                            "product_id": data["key"][2]["code_batch"][0]["product"]["legal"][0]["product_id"],
                            "product_type": data["key"][2]["code_batch"][0]["product"]["legal"][0]["product_type"],
                            "product_name": data["key"][2]["code_batch"][0]["product"]["legal"][0]["product_name"],
                            "contract_price": data["key"][2]["code_batch"][0]["product"]["legal"][0]["contract_price"],
                            "cost_price": data["key"][2]["code_batch"][0]["product"]["legal"][0]["cost_price"],
                            "official_price": data["key"][2]["code_batch"][0]["product"]["legal"][0]["official_price"],
                            "quantity": data["key"][2]["code_batch"][0]["product"]["legal"][0]["quantity"],
                            "show_url": data["key"][2]["code_batch"][0]["product"]["legal"][0]["show_url"],
                            "describe_url": data["key"][2]["code_batch"][0]["product"]["legal"][0]["describe_url"],
                            "detail_url": "",
                            "account_type": data["key"][2]["code_batch"][0]["product"]["legal"][0]["account_type"],
                            "map_product_name": data["key"][2]["code_batch"][0]["product"]["legal"][0][
                                "map_product_name"],
                            "only": data["key"][2]["code_batch"][0]["product"]["legal"][0]["only"],
                            "weight": data["key"][2]["code_batch"][0]["product"]["legal"][0]["weight"],
                            "type": data["key"][2]["code_batch"][0]["product"]["legal"][0]["type"],
                            "upstream": data["key"][2]["code_batch"][0]["product"]["legal"][0]["upstream"]
                        }, {
                            "product_id": data["key"][2]["code_batch"][0]["product"]["legal"][1]["product_id"],
                            "product_type": data["key"][2]["code_batch"][0]["product"]["legal"][1]["product_type"],
                            "product_name": data["key"][2]["code_batch"][0]["product"]["legal"][1]["product_name"],
                            "contract_price": data["key"][2]["code_batch"][0]["product"]["legal"][1]["contract_price"],
                            "cost_price": data["key"][2]["code_batch"][0]["product"]["legal"][1]["cost_price"],
                            "official_price": data["key"][2]["code_batch"][0]["product"]["legal"][1]["official_price"],
                            "quantity": data["key"][2]["code_batch"][0]["product"]["legal"][1]["quantity"],
                            "show_url": data["key"][2]["code_batch"][0]["product"]["legal"][1]["show_url"],
                            "describe_url": data["key"][2]["code_batch"][0]["product"]["legal"][1]["describe_url"],
                            "detail_url": "",
                            "account_type": data["key"][2]["code_batch"][0]["product"]["legal"][1]["account_type"],
                            "map_product_name": data["key"][2]["code_batch"][0]["product"]["legal"][1][
                                "map_product_name"],
                            "only": data["key"][2]["code_batch"][0]["product"]["legal"][1]["only"],
                            "weight": data["key"][2]["code_batch"][0]["product"]["legal"][1]["weight"],
                            "type": data["key"][2]["code_batch"][0]["product"]["legal"][1]["type"],
                            "upstream": data["key"][2]["code_batch"][0]["product"]["legal"][1]["upstream"]
                        }],
                        "reduce": []
                    }
                }],
                "mobile_excel": "",
                "mobile_repeat": []
            }, {
                "batch_name": data["key"][3]["batch_name"],
                "style": data["key"][3]["style"],
                "quantity": data["key"][3]["quantity"],
                "bind_object": data["key"][3]["bind_object"],
                "allow_repetition": data["key"][3]["allow_repetition"],
                "allow_loss": data["key"][3]["allow_loss"],
                "merge_stock": data["key"][3]["merge_stock"],
                "code_batch": [{
                    "title": data["key"][3]["code_batch"][0]["title"],
                    "describe": "",
                    "begin_time": str(begin_time),
                    "end_time": str(end_time),
                    "quantity": data["key"][3]["code_batch"][0]["quantity"],
                    "restrict": data["key"][3]["code_batch"][0]["restrict"],
                    "range": data["key"][3]["code_batch"][0]["range"],
                    "product": {
                        "legal": [{
                            "product_id": data["key"][3]["code_batch"][0]["product"]["legal"][0]["product_id"],
                            "product_type": data["key"][3]["code_batch"][0]["product"]["legal"][0]["product_type"],
                            "product_name": data["key"][3]["code_batch"][0]["product"]["legal"][0]["product_name"],
                            "contract_price": data["key"][3]["code_batch"][0]["product"]["legal"][0]["contract_price"],
                            "cost_price": data["key"][3]["code_batch"][0]["product"]["legal"][0]["cost_price"],
                            "official_price": data["key"][3]["code_batch"][0]["product"]["legal"][0]["official_price"],
                            "quantity": data["key"][3]["code_batch"][0]["product"]["legal"][0]["quantity"],
                            "show_url": data["key"][3]["code_batch"][0]["product"]["legal"][0]["show_url"],
                            "describe_url": data["key"][3]["code_batch"][0]["product"]["legal"][0]["describe_url"],
                            "detail_url": "",
                            "account_type": data["key"][3]["code_batch"][0]["product"]["legal"][0]["account_type"],
                            "map_product_name": data["key"][3]["code_batch"][0]["product"]["legal"][0][
                                "map_product_name"],
                            "only": data["key"][3]["code_batch"][0]["product"]["legal"][0]["only"],
                            "weight": data["key"][3]["code_batch"][0]["product"]["legal"][0]["weight"],
                            "type": data["key"][3]["code_batch"][0]["product"]["legal"][0]["type"],
                            "upstream": data["key"][3]["code_batch"][0]["product"]["legal"][0]["upstream"]
                        }],
                        "reduce": []
                    }
                }],
                "mobile_excel": "",
                "mobile_repeat": []
            }],
            "theme": {
                "theme_id": 801,
                "use": 1,
                "link": 3
            },
            "complete": "true"
        }
        resp = requests.put(url=host + url + str(plan_id), json=request_param, headers=headers)
        print("第二步：", resp.json())

        return plan_id


if __name__ == "__main__":
    MarketingPlanSteptwo().marketing_plan_step_two()
